﻿using System;
using System.Collections.Generic;
using System.Xml.Serialization;
using Framework.Interface;

namespace Framework.DataObjects
{
    /// <summary>
    /// Name/Value data object
    /// </summary>
    public class NameValueDo : DomObject, INameValue
    {
        #region C'tor
        public NameValueDo()
        {
        }

        public NameValueDo(string name, string value)
        {
            Name = name;
            Value = value;
        }
        #endregion

        [XmlAttribute("Name")]
        public string Name { get; set; }

        [XmlAttribute("Value")]
        public string Value { get; set; }

        #region ShallowCopy( )
        public NameValueDo ShallowCopy()
        {
            return (NameValueDo)MemberwiseClone();
        }
        #endregion

        // *********************************************************************************
        // Static Helper
        // *********************************************************************************

        #region CreateFromStringArray
        /// <summary>
        /// Creates a list of NameTextValueDo objects from a string/value pair array.
        /// </summary>
        /// <param name="nameValuePairs">The name value pairs.</param>
        /// <returns></returns>
        public static List<NameValueDo> CreateFromStringArray(params string[] nameValuePairs)
        {
            if (Is.EmptyArray(nameValuePairs))
            {
                return new List<NameValueDo>();
            }

            if (nameValuePairs.Length % 2 != 0)
            {
                throw new Exception("");
            }

            var result = new List<NameValueDo>();

            for (var i = 0; i < nameValuePairs.Length; i += 2)
            {
                result.Add(new NameValueDo(nameValuePairs[i], nameValuePairs[i + 1]));
            }

            return result;
        }
        #endregion
    }
}
